package cn.school.controller.course;

import cn.hutool.core.util.StrUtil;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.utils.SysUserUtils;
import cn.school.course.entity.CoursePlan;
import cn.school.course.service.CoursePlanService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排课计划
 *
 * @className: CoursePlanController
 * @author: fangjin.tao
 * @date: 2023/1/6
 **/
@RestController
@RequestMapping("/course/plan")
@Api(tags = "CoursePlan Controller => 排课计划管理")
public class CoursePlanController {
	@Autowired
	private CoursePlanService coursePlanService;
	
	/**
	 * @throws
	 * @author fangjin.tao
	 * @param: params
	 * @updateTime 2023/1/6 9:38
	 * @return: cn.school.common.api.CommonResult<cn.school.common.api.CommonPage < cn.school.course.entity.CoursePlan>>
	 */
	@ApiOperation("查询排课计划列表")
	@GetMapping("list")
	public CommonResult<CommonPage<CoursePlan>> list(@RequestParam HashMap<String, Object> params) {
		String pageNum = (String) params.get("pageNum");
		String pageSize = (String) params.get("pageSize");
		String tetiId = (String) params.get("tetiId"); // 学年学期ID
		String subjId = (String) params.get("subjId"); // 科目ID
		String scclId = (String) params.get("scclId"); // 班级ID
		String coseId = (String) params.get("coseId"); // 节次ID
		String syusId = (String) params.get("syusId"); // 教员用户ID
		String bsloId = (String) params.get("bsloId"); // 场所教室ID
		
		IPage<CoursePlan> page = new Page(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		LambdaQueryWrapper<CoursePlan> queryWrapper = new LambdaQueryWrapper<>();
		//租户
		queryWrapper.eq(CoursePlan::getTenantId, SysUserUtils.getSysUser().getTenantId());
		if (StrUtil.isNotEmpty(tetiId)) {
			queryWrapper.eq(CoursePlan::getTetiId, Long.parseLong(tetiId));
		}
		if (StrUtil.isNotEmpty(subjId)) {
			queryWrapper.eq(CoursePlan::getSubjId, Long.parseLong(subjId));
		}
		if (StrUtil.isNotEmpty(scclId)) {
			queryWrapper.eq(CoursePlan::getScclId, Long.parseLong(scclId));
		}
		if (StrUtil.isNotEmpty(coseId)) {
			queryWrapper.eq(CoursePlan::getCoseId, Long.parseLong(coseId));
		}
		if (StrUtil.isNotEmpty(syusId)) {
			queryWrapper.eq(CoursePlan::getSyusId, Long.parseLong(syusId));
		}
		if (StrUtil.isNotEmpty(bsloId)) {
			queryWrapper.eq(CoursePlan::getBsloId, Long.parseLong(bsloId));
		}
		page = coursePlanService.page(page, queryWrapper);
		if (page.getRecords() != null && page.getRecords().size() != 0) {
			List<String> weekDayList = new ArrayList<>();
			for (CoursePlan record : page.getRecords()) {
				weekDayList = Arrays.stream(record.getCoplWeekDayNos().split(",")).map(s->s.toString()).collect(Collectors.toList());
				record.setCoplWeekDayNoList(weekDayList);
			}
			
		}
		return CommonResult.success(CommonPage.restPage(page));
	}
	
	/**
	 * @throws
	 * @author fangjin.tao
	 * @param: coursePlan
	 * @updateTime 2023/1/6 14:36
	 * @return: cn.school.common.api.CommonResult<java.lang.String>
	 */
	@ApiOperation("创建或更新排课计划")
	@PostMapping("/createOrUpdate")
	public CommonResult<String> createOrUpdate(@RequestBody CoursePlan coursePlan) {
		coursePlan.setCoplWeekDayNos(coursePlan.getCoplWeekDayNoList().stream().map(weekdayNo -> weekdayNo.toString()).collect(Collectors.joining(",")));
		this.coursePlanService.saveOrUpdateBiz(coursePlan);
		return CommonResult.success();
	}
	
	/**
	 * @throws
	 * @author fangjin.tao
	 * @param: coursePlan
	 * @updateTime 2023/2/7 15:17
	 * @return: cn.school.common.api.CommonResult<java.lang.String>
	 */
	@ApiOperation("创建或更新课程")
	@DeleteMapping("/delete/{id}")
	public CommonResult<String> delete(@PathVariable("id") Long coplId) {
		this.coursePlanService.deleteCoursePlan(coplId);
		return CommonResult.success();
	}
}
